package com.example.demo.common.config;


import com.example.demo.common.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;


@Configuration
public class IntercepterConfig implements WebMvcConfigurer {

    private TokenInterceptor tokenInterceptor;

    //构造方法
    public IntercepterConfig(TokenInterceptor tokenInterceptor){
        this.tokenInterceptor = tokenInterceptor;
    }


    /*
    *
    * 拦截器配置
    *
    * */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePath = new ArrayList<>();
        excludePath.add("/login");//登录
        excludePath.add("/logout");//登出
        excludePath.add("/static/**");//静态资源
        excludePath.add("/swagger-ui.html/**");//静态资源
        excludePath.add("/assrts/**");//静态资源
        excludePath.add("/dataPath/**");
        excludePath.add("/sharePath/**");
          excludePath.add("/DFileLoadDown");
          excludePath.add("/SFileLoadDown");
        excludePath.add("/user/**");

        registry.addInterceptor(tokenInterceptor).addPathPatterns("/**").excludePathPatterns(excludePath);
        WebMvcConfigurer.super.addInterceptors(registry);
    }


    /*
    * 跨域支持
    *
    * */

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //添加映射路径
        registry.addMapping("/**")
                //放行哪些原始域
                .allowedOrigins("*")
                //是否发送Cookie信息
                .allowCredentials(true)
                //放行哪些原始域（请求方式）
                .allowedMethods("GET","POST","DELETE","PUT","PATCH","OPTIONS","HEAD")
                //放行哪些头部信息
                .allowedHeaders("*")
                .exposedHeaders("access-control-allow-headers",
                        "access-control-allow-methods",
                        "access-control-allow-origin",
                        "access-control-max-age",
                        "X-Frame-Options")
                .maxAge(3600 * 24);
    }
}